package com.tang.auth.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.tang.auth.domain.convert.AuthRoleBOConverter;
import com.tang.auth.domain.entity.AuthRoleBO;
import com.tang.auth.domain.service.AuthRoleDomainService;
import com.tang.auth.infra.basic.entity.AuthRole;
import com.tang.auth.infra.basic.service.AuthRoleService;
import com.tang.common.enums.DeleteEnum;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Objects;

@Slf4j
@Service
public class AuthRoleDomainServiceImpl implements AuthRoleDomainService {

    @Resource
    private AuthRoleService authRoleService;

    /**
     * 添加角色信息
     *
     * @param authRoleBO 待添加的角色信息
     * @return java.lang.Boolean
     * @author Tang
     * @date 2024/2/15 11:20
     **/
    @Override
    @SneakyThrows
    @Transactional(rollbackFor = Exception.class)
    public Boolean addRole(AuthRoleBO authRoleBO) {
        if (log.isInfoEnabled()) {
            log.info("com.tang.auth.domain.service.impl.AuthRoleDomainServiceImpl.addRole.bo:{}", JSON.toJSONString(authRoleBO));
        }
        AuthRole authRole = AuthRoleBOConverter.INSTANCE.convertBOToEntity(authRoleBO);
        authRole.setIsDeleted(DeleteEnum.UN_DELETED.getCode());
        AuthRole insert = authRoleService.insert(authRole);
        return Objects.nonNull(insert.getId());
    }

    /**
     * 更新角色信息
     *
     * @param authRoleBO 待添加的角色信息
     * @return java.lang.Boolean
     * @author Tang
     * @date 2024/2/15 11:20
     **/
    @Override
    @SneakyThrows
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateRole(AuthRoleBO authRoleBO) {
        if (log.isInfoEnabled()) {
            log.info("com.tang.auth.domain.service.impl.AuthRoleDomainServiceImpl.updateRole.bo:{}", JSON.toJSONString(authRoleBO));
        }
        AuthRole authRole = AuthRoleBOConverter.INSTANCE.convertBOToEntity(authRoleBO);
        return authRoleService.update(authRole);
    }

    /**
     * 删除角色信息
     *
     * @param authRoleBO 待删除的角色信息
     * @return java.lang.Boolean
     * @author Tang
     * @date 2024/2/15 11:26
     **/
    @Override
    @SneakyThrows
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteRole(AuthRoleBO authRoleBO) {
        if (log.isInfoEnabled()) {
            log.info("com.tang.auth.domain.service.impl.AuthRoleDomainServiceImpl.deleteRole.bo:{}", JSON.toJSONString(authRoleBO));
        }
        AuthRole authRole = AuthRoleBOConverter.INSTANCE.convertBOToEntity(authRoleBO);
        authRole.setIsDeleted(DeleteEnum.DELETE.getCode());
        return authRoleService.update(authRole);
    }


}
